package com.nike.amoeba.ui.frg

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.kingja.loadsir.core.LoadService
import com.nike.amoeba.adapter.StockAdjAdapter
import com.nike.amoeba.api.UIHelper
import com.nike.amoeba.base.BaseFragment
import com.nike.amoeba.bean.DataBean
import com.nike.amoeba.databinding.FStockOrderBinding
import com.nike.amoeba.ext.STOCK_ADJ_ORDER_TYPE
import com.nike.amoeba.ext.init
import com.nike.amoeba.ext.loadListData
import com.nike.amoeba.ext.loadServiceInit
import com.nike.amoeba.ext.setNbOnItemClickListener
import com.nike.amoeba.ext.showLoading
import com.nike.amoeba.mar.eventViewModel
import com.nike.amoeba.viewmodel.RfidModel
import com.nike.amoeba.viewmodel.StockAdjustModel
import com.nike.amoeba.weight.recyclerview.SpaceItemDecoration
import me.hgj.jetpackmvvm.ext.nav

/**
 * User: Nike
 *  2024/5/28 10:25
 *      库存调整
 */
class StockAdjustFrg: BaseFragment<StockAdjustModel, FStockOrderBinding>() {

    val adapter: StockAdjAdapter by lazy { StockAdjAdapter(arrayListOf()) }

    lateinit var loadsir: LoadService<Any>

    val rfidModel: RfidModel by activityViewModels()

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            mViewModel.title.set(it.getString("title"))
            mViewModel.code.set(it.getString("code"))
        }
        //状态页配置
        loadsir = loadServiceInit(mDatabind.swipeRefresh) {
            lazyLoadData()
        }

        //初始化recyclerView
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(20f), ConvertUtils.dp2px(20f), true))
        }

        adapter.run {
            setNbOnItemClickListener{adapter, view, position ->
                val bean = adapter.data[position] as DataBean
                UIHelper.startStockAdjListFrg(nav(), bean.RoNo!!, bean.AssetNo, mViewModel.code.get())
            }
        }

        mDatabind.swipeRefresh.isRefreshing = false
        mDatabind.swipeRefresh.setOnRefreshListener {
            mViewModel.onRequest(mViewModel.code.get())
        }

        mDatabind.includeSearch.etText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                adapter!!.filter.filter(p0.toString())
            }

            override fun afterTextChanged(p0: Editable?) {
            }
        })

        mDatabind.includeSearch.ivQr.setOnClickListener {
            UIHelper.startZxingAct(STOCK_ADJ_ORDER_TYPE)
        }

        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                setToolBarTitle(mViewModel.title.get())
            }
        })
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listBean.observe(viewLifecycleOwner, Observer {
            loadListData(it, adapter, loadsir, mDatabind.recyclerView, mDatabind.swipeRefresh, it.pageSize)
            adapter!!.appendList(it.listData)
        })
        //扫码
        eventViewModel.zkingType.observeInFragment(this, Observer {dataBean ->
            if (dataBean.Type == STOCK_ADJ_ORDER_TYPE){
                val bean = adapter.data.firstOrNull { it.AssetNo == dataBean.text }
                bean?.let { UIHelper.startStockAdjListFrg(nav(), bean.RoNo!!, bean.AssetNo, mViewModel.code.get()) }
            }
        })
        rfidModel.scanData.observe(viewLifecycleOwner, { scanData ->
            if (scanData != null && viewLifecycleOwner.lifecycle.currentState == Lifecycle.State.RESUMED) {
                eventViewModel.zkingType.value = DataBean(Type = STOCK_ADJ_ORDER_TYPE, text = scanData)
            }
        })
        eventViewModel.stockAdjustUploadEvent.observeInFragment(this, Observer {
            mDatabind.swipeRefresh.isRefreshing = true
            mViewModel.title.set(it.Title)
            mViewModel.code.set(it.Code)
            mViewModel.onRequest(mViewModel.code.get())
            setToolBarTitle(mViewModel.title.get())
        })
    }


    override fun lazyLoadData() {
        super.lazyLoadData()
        //设置界面 加载中
        loadsir.showLoading()
        mViewModel.onRequest(mViewModel.code.get())
    }

}